<template>
  <el-container>
    <el-main>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55"> </el-table-column> -->

        <el-table-column prop="employeeName" label="员工姓名" width="120">
        </el-table-column>
        <el-table-column prop="baseSalary" label="基本工资" width="120">
        </el-table-column>
        <el-table-column prop="deptName" label="部门名称" width="120">
        </el-table-column>
        <el-table-column prop="jobName" label="职位" width="120">
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              v-has="'ROLE_admin'"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <!--
            <el-button
              size="mini"
              type="danger"
              v-has="'ROLE_admin'"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
           -->
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="修改职位与基本工资" :visible.sync="dialogFormVisible">
        <el-form :model="employee">
          <el-form-item label="姓名">
            <el-input
              v-model="employee.employeeName"
              style="width: 250px"
            ></el-input>
          </el-form-item>
          <el-form-item label="部门">
            <el-select
              v-model="employee.deptId"
              placeholder="请选择用户所属部门"
            >
              <el-option
                v-for="item in deptData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="岗位">
            <el-select
              v-model="employee.jobId"
              placeholder="请选择用户所属岗位"
            >
              <el-option
                v-for="item in jobData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="基础工资">
            <el-input
              v-model="employee.baseSalary"
              style="width: 250px"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleEditCancel">取 消</el-button>
          <el-button type="primary" @click="handleEditCommit">确 定</el-button>
        </div>
      </el-dialog>
    </el-main>

    <!-- 页脚分页 -->
    <div class="fenye">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-container>
</template>

<script>
export default {
  name: "Employee",
  data() {
    return {
      allData: [],
      tableData: [],
      page: 1, //页脚显示的当前页
      size: 10, //一页多少条数据
      total: 400, //总条目数
      multipleSelection: [], //当前选中的行
      dialogFormVisible: false, //弹出的修改表单
      jobData: "", //本公司的职位信息
      deptData: "", //部门信息
      employee: {
        id: "",
        employeeName: "",
        userId: "",
        deptId: "",
        jobId: "",
        baseSalary: "",
      },
      formLabelWidth: "120px", //宽度
    };
  },
  methods: {
    //获取所有部门信息
    getAllDept() {
      this.$request.get("dept/queryAll").then((res) => {
        // console.log(res);
        if (res.data.obj != null) {
          this.deptData = res.data.obj;
          console.log(this.deptData);
        } else {
          this.$message.error("信息获取失败");
        }
      });
    },
    //获取所有的职位信息
    getAllJob() {
      this.$request.get("job/queryAll").then((res) => {
        if (res.data.obj != null) {
          this.jobData = res.data.obj;
          // console.log(this.jobData);
        } else {
          this.$message.error("信息获取失败");
        }
      });
    },
    getAllEmployee() {
      this.$request.get("employee/queryAllEmployeeSalary").then((res) => {
        if (res.data.obj != null) {
          this.allData = res.data.obj;
          this.getTableData();
        } else {
          this.$message.error("信息获取失败");
        }
      });
    },

    //分页表格数据
    getTableData() {
      //allData为全部数据
      this.tableData = this.allData.slice(
        (this.page - 1) * this.size,
        this.page * this.size
      );
      console.log(this.tableData);
      this.total = this.allData.length; //总条目等于allDaTa长度
    },
    //size改变时的回调，参数为当前的size
    handleSizeChange(val) {
      // console.log("改变每页多少条，当前一页多少条", val);
      this.size = val;
      this.page = 1; //size改变时要把page重置为1，再把参数赋值给size，因为size改变会导致page数量改变
      this.getTableData();
    },
    //page改变时的回调函数，参数为当前页码
    handleCurrentChange(val) {
      // console.log("翻页，当前为第几页", val);
      this.page = val;
      this.getTableData();
    },
    reset() {},
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.dialogFormVisible = true;
      this.employee.id = row.employeeId;
      this.employee.employeeName = row.employeeName;
      this.employee.deptId = row.deptId;
      this.employee.jobId = row.jobId;
      this.employee.baseSalary = row.baseSalary;
    },

    handleEditCommit() {
      this.$request
        .post("employee/insert", {
          id: this.employee.id,
          deptId: this.employee.deptId,
          jobId: this.employee.jobId,
          baseSalary: this.employee.baseSalary,
        })
        .then((res) => {
          if (res.data.obj) {
            this.$message.success("修改成功");
            this.employee.deptId = "";
            this.employee.jobId = "";
            this.employee.baseSalary = "";
            this.reset();
          } else {
            this.$message.warning("修改失败");
          }
        });

      this.dialogFormVisible = false;
    },
    handleEditCancel() {
      this.dialogFormVisible = false;
    },
  },
  mounted() {
    this.getAllEmployee();
    this.getTableData();
    this.getAllJob();
    this.getAllDept();
  },
};
</script>

<style  scoped>
.el-header {
  background-color: rgb(42, 53, 66);
  color: #fff;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
.el-col {
  /* background-color: #fff; */
  color: #fff;
}
i {
  font-size: 18px;
}
.fenye {
  position: fixed;
  bottom: 10px;
}
</style>